package com.woniuxy.house.service.serviceimpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.house.entity.PageBean;
import com.woniuxy.house.entity.Post;
import com.woniuxy.house.entity.PostDetail;
import com.woniuxy.house.entity.PostPictures;
import com.woniuxy.house.mapper.PostDetailMapper;
import com.woniuxy.house.mapper.PostMapper;
import com.woniuxy.house.mapper.PostPictureMapper;
import com.woniuxy.house.service.PostService;
import com.woniuxy.house.vo.ResponseEntity;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
/**
 * 首页租友求组功能的接口实现类
 */
@Service("tenantAskServiceImpl")
@Transactional
public class PostServiceImpl implements PostService {
    @Autowired
   private PostMapper postMapper;
    @Autowired
    private PostDetailMapper postDetailMapper;
    @Autowired
    private PostPictureMapper postPictureMapper;
    @Override
    public PageBean<Post> getByPage(int page) {
        //根据条件页码分页大小将PageBean中的属性一次设置
        PageBean<Post> pageBean = new PageBean<>();
        pageBean.setPageSzie(2);//分页大小
        pageBean.setCurrPage(page);    //设置当前页数
        Page p = PageHelper.startPage(pageBean.getCurrPage(), pageBean.getPageSzie());
        PageHelper.orderBy("time desc");
        List<Post> list = postMapper.getAll();
       // List<Post> list = postMapper.getAllByDesc();
        pageBean.setTotalNums((int) p.getTotal());//设置总数量
        pageBean.setTotalPage(p.getPages());
        pageBean.setData(list);//设置当前页数的数据
        pageBean.setCurrpageSzie(pageBean.getData().size());//当前页数据数量
        return pageBean;
    }
    @Override
    public PageBean<Post> getPageBySearchText(String searchText,Integer page) {
        Post post=new Post();
        post.setTitle(searchText);
        //根据条件页码分页大小将PageBean中的属性一次设置
        PageBean<Post> pageBean = new PageBean<>();
        pageBean.setPageSzie(6);//分页大小
        pageBean.setCurrPage(page);    //设置当前页数
        Page p = PageHelper.startPage(pageBean.getCurrPage(), pageBean.getPageSzie());
        PageHelper.orderBy("time desc");
        List<Post> list = postMapper.getBySearchText(post);
        pageBean.setTotalNums((int) p.getTotal());//设置总数量
        pageBean.setTotalPage(p.getPages());
        pageBean.setData(list);//设置当前页数的数据
        pageBean.setCurrpageSzie(pageBean.getData().size());//当前页数据数量
        return pageBean;

    }

    @Override
    public PageBean<Post> getPageByCondition(Post post, Integer page) {
        //根据条件页码分页大小将PageBean中的属性一次设置
        PageBean<Post> pageBean = new PageBean<>();
        pageBean.setPageSzie(2);//分页大小
        pageBean.setCurrPage(page);    //设置当前页数
        Page p = PageHelper.startPage(pageBean.getCurrPage(), pageBean.getPageSzie());
        List<Post> list = postMapper.getBySearchText(post);
        pageBean.setTotalNums((int) p.getTotal());//设置总数量
        pageBean.setTotalPage(p.getPages());
        pageBean.setData(list);//设置当前页数的数据
        pageBean.setCurrpageSzie(pageBean.getData().size());//当前页数据数量
        return pageBean;
    }

    @Override
    public  void addQiuZuPost(Post post, PostDetail postDetail) {
postDetailMapper.add(postDetail);
post.setTime(LocalDateTime.now().toString());
post.setSign("q");
post.setIsDelete("n");
post.setIsAcross("n");
post.setPostDetail(postDetail);
postMapper.addPost(post);


    }

    @Override
    public void addZhaoZuPost(Post post, PostDetail postDetail, List<String >img) {
        postDetailMapper.add(postDetail);
        post.setTime(LocalDateTime.now().toString());
        post.setSign("z");
        post.setIsDelete("n");
        post.setIsAcross("n");
        post.setPostDetail(postDetail);
        postMapper.addPost(post);
        PostPictures postPictures=new PostPictures();
        for(String i :img){
            postPictures.setPicture(i);
            postPictures.setPost(post);
            postPictureMapper.add(postPictures);
        }

    }

    @Override
    public void updateByCondition(Post post) {
        postMapper.updateByCondition(post);
    }

    @Override
    public PageBean<Post> getByUserIdPost(Integer uid,Integer page) {
        //根据条件页码分页大小将PageBean中的属性一次设置
        PageBean<Post> pageBean = new PageBean<>();
        pageBean.setPageSzie(6);//分页大小
        pageBean.setCurrPage(page);    //设置当前页数
        Page p = PageHelper.startPage(pageBean.getCurrPage(), pageBean.getPageSzie());
        PageHelper.orderBy("time desc");
        List<Post> list = postMapper.getByUserIdPost(uid);
        pageBean.setTotalNums((int) p.getTotal());//设置总数量
        pageBean.setTotalPage(p.getPages());
        pageBean.setData(list);//设置当前页数的数据
        pageBean.setCurrpageSzie(pageBean.getData().size());//当前页数据数量
        return pageBean;
    }
}
